package com.spottydog.virtues;

import android.app.Activity;
import android.content.Context;
import android.util.AttributeSet;
import android.widget.ImageView;
import android.widget.LinearLayout;

public class PenMarkLine extends LinearLayout {

	private static final int LENGTH = 10;
	
	private OnValuesChangedListener listener = null;
	
	private enum Value { BLOT, PLUS, NONE };
	private Value[] values = null;
	
	public PenMarkLine(Context context) {
		this(context, null);
	}
	
	public PenMarkLine(Context context, AttributeSet attrs) {
		super(context, attrs);
		
		this.setOrientation(LinearLayout.HORIZONTAL);
		this.setPadding(10, 10, 10, 10);	
		
		((Activity) getContext()).getLayoutInflater().inflate(R.layout.pen_mark_line, this, true);
		
		resetValues();
		updateImages();
		setValuesString("-+-++");
	}
	
	private void resetValues() {
		this.values = new Value[LENGTH];
		for (int i = 0; i < values.length; i++) values[i] = Value.NONE;
	}
	
	public void setValuesString(String valuesString) {
		resetValues();
		for (int i = 0; i < values.length && i < valuesString.length(); i++) {
			char c = valuesString.charAt(i);
			if (c == '+') values[i] = Value.PLUS;
			else if (c == '-') values[i] = Value.BLOT;
			else throw new IllegalArgumentException("Values string must be + or - only");
		}
		updateImages();
	}
	
	public String getValuesString() {
		StringBuilder sb = new StringBuilder();
		for (Value v : values) {
			if (v == Value.PLUS) sb.append('+');
			else if (v == Value.BLOT) sb.append('-');
			else break;
		}
		return sb.toString();
	}
	
	public OnValuesChangedListener getOnValuesChangedListener() {
		return listener;
	}

	public void setOnValuesChangedListener(OnValuesChangedListener listener) {
		this.listener = listener;
	}
	
	public void addBlot() {
		for (int i = 0; i < this.values.length; i++) {
			if (values[i] == Value.NONE) {
				values[i] = Value.BLOT;
				break;
			}
		}
		updateImages();
		if (listener != null) listener.onValuesChanged(getValuesString(), getCount());
	}
	
	public void addPlus() {
		for (int i = 0; i < this.values.length; i++) {
			if (values[i] == Value.NONE) {
				values[i] = Value.PLUS;
				break;
			}
		}
		updateImages();
		if (listener != null) listener.onValuesChanged(getValuesString(), getCount());
	}
	
	public void removeLast() {
		for (int i = 1; i < this.values.length; i++) {
			if (values[i] == Value.NONE) {
				values[i-1] = Value.NONE;
				break;
			}
		}
		updateImages();
		if (listener != null) listener.onValuesChanged(getValuesString(), getCount());
	}
	
	public void clear() {
		resetValues();
		updateImages();
		if (listener != null) listener.onValuesChanged(getValuesString(), getCount());		
	}
	
	public int getCount() {
		int count = 0;
		for (Value v : values) {
			if (v == Value.PLUS) count++;
			else if (v == Value.BLOT) count--; 
		}
		return count;
	}
	
	private void updateImages() {
		for (int i = 0; i < this.values.length; i++) {
			int img = R.drawable.blank;
			if (values[i] == Value.PLUS) img = R.drawable.plus;
			else if (values[i] == Value.BLOT) img = R.drawable.blot;
			
			((ImageView) this.getChildAt(i)).setImageResource(img);
		}
	}
	
	public boolean isFull() {
		for (Value v : values) {
			if (v == Value.NONE) return false;
		}
		return true;
	}
	
	public boolean isEmpty() {
		for (Value v : values) {
			if (v != Value.NONE) return false;
		}
		return true;
	}
	
	public interface OnValuesChangedListener {
		public void onValuesChanged(String values, int count);
	}
}
